package view;

/**
 * Created by xulei on 2018/4/4.
 */

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import bean.ItemBean;
import student.andlei.com.student.R;
import view.doodle.ActionTypeEnum;
import view.doodle.DoodleView;

/**
 * 弹窗视图
 */
public class LiveSelectPenPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity context;
    private RelativeLayout layout;
    private DoodleView doodleView;
    private RadioGroup group_size,group_color;
    private RadioButton rbtn_pensize_one,
                        rbtn_pensize_two,
                        rbtn_pensize_three,
                        rbtn_pensize_four,
                        rbtn_pensize_five;
    private RadioButton rbtn_pencolor_one,
                        rbtn_pencolor_two,
                        rbtn_pencolor_three,
                        rbtn_pencolor_four,
                        rbtn_pencolor_five;
    private Button btn_cannle,btn_sure;

    public LiveSelectPenPopWindow(Activity context, DoodleView doodleView) {
        super(context);
        this.context = context;
        this.doodleView = doodleView;
        initalize();
    }
    public LiveSelectPenPopWindow(Activity context, List<ItemBean> list) {
        super(context);
        this.context = context;
        initalize();
    }
    public LiveSelectPenPopWindow(Activity context, List<ItemBean> list, TextView editText) {
        super(context);
        this.context = context;
        initalize();
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.popup_live_selectpen, null);
        layout = view.findViewById(R.id.rlayout_);
        group_size = view.findViewById(R.id.group_size);
        group_color = view.findViewById(R.id.group_color);
        //-----------画笔大小
        rbtn_pensize_one  = view.findViewById(R.id.radio_pensize_one);
        rbtn_pensize_two  = view.findViewById(R.id.radio_pensize_two);
        rbtn_pensize_three = view.findViewById(R.id.radio_pensize_three);
        rbtn_pensize_four = view.findViewById(R.id.radio_pensize_four);
        rbtn_pensize_five = view.findViewById(R.id.radio_pensize_five);
        //-------------画笔颜色
        rbtn_pencolor_one = view.findViewById(R.id.radio_pencolor_one);
        rbtn_pencolor_two = view.findViewById(R.id.radio_pencolor_two);
        rbtn_pencolor_three = view.findViewById(R.id.radio_pencolor_three);
        rbtn_pencolor_four = view.findViewById(R.id.radio_pencolor_four);
        rbtn_pencolor_five = view.findViewById(R.id.radio_pencolor_five);
        //-----------取消 确定
        btn_cannle = view.findViewById(R.id.btn_cancel);
        btn_sure = view.findViewById(R.id.btn_sure);

        setContentView(view);
        initWindow();
        if(doodleView == null){
            dismiss();
            return;
        }
        btn_cannle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doodleView.setPaintType(ActionTypeEnum.Path.getValue());
                if(rbtn_pensize_one.isChecked()){
                    doodleView.setPaintSize(2);
                }else if(rbtn_pensize_two.isChecked()){
                    doodleView.setPaintSize(4);
                }else if(rbtn_pensize_three.isChecked()){
                    doodleView.setPaintSize(6);
                }else if(rbtn_pensize_four.isChecked()){
                    doodleView.setPaintSize(8);
                }else if(rbtn_pensize_five.isChecked()){
                    doodleView.setPaintSize(10);
                }
                if(rbtn_pencolor_one.isChecked()){
                    doodleView.setPaintColor(0xFFFF432F);
                }else if(rbtn_pencolor_two.isChecked()){
                    doodleView.setPaintColor(0xFFF5AF22);
                }else if(rbtn_pencolor_three.isChecked()){
                    doodleView.setPaintColor(0xFF2fbeff);
                }else if(rbtn_pencolor_four.isChecked()){
                    doodleView.setPaintColor(0xFF101010);
                }else if(rbtn_pencolor_five.isChecked()){
                    doodleView.setPaintColor(0xFFF4F3F3);
                }
                dismiss();
            }
        });
//        layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dismiss();
//            }
//        });

    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                backgroundAlpha((Activity) context, 1f);
//            }
//        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 10);
        //showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    @Override
    public void onClick(View view) {

    }
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**
     * * Checks if the app has permission to write to device storage
     * *
     * * If the app does not has permission then the user will be prompted to
     * * grant permissions
     * *
     * * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
